clear all
set mem 100m

local path0="C:\Dropbox\GeneticsProject (1)\REStat\round_accepted_replication_files\"
local path1="C:\Dropbox\GeneticsProject (1)\REStat\round_accepted_replication_files\outregs"

cd "`path0'workfiles"

use completed_data, clear
keep country countrycode idv distE_USA distE_UK distM_USA distM_UK 


replace country="Korea" if country=="Korea, Republic of"
replace country="Bosnia and Herz." if country=="Bosnia and Herzegovina"
replace country="Central African Rep." if country=="Central African Republic"
replace country="Côte d'Ivoire" if country=="Cote d`Ivoire"
replace country="Czech Rep." if country=="Czech Republic"
replace country="Dominican Rep." if country=="Dominican Republic"
replace country="Gambia" if country=="Gambia, The"
replace country="Lao PDR" if country=="Laos"
replace country="Serbia" if country=="Serbia and Montenegro"
replace country="Slovakia" if country=="Slovak Republic"
replace country="Trinidad and Tobago" if country=="Trinidad &Tobago"
replace country="Dem. Rep. Congo" if country=="Congo, Dem. Rep."
replace country="Congo" if country=="Congo, Republic of"

replace country = proper(country)

replace country="United States" if country=="Usa"
replace country="United Kingdom" if country=="Uk"
replace country="Serbia" if country=="Serbia-Montenegro"
replace country="Bosnia And Herz." if country=="Bosnia"
replace country="Czech Rep."  if country=="Czechrep"
replace country="Germany" if country=="Germanyfed. Rep."
replace country="Guinea-Bissau" if country=="Guinea Bissau"

replace country="Lao Pdr" if country=="Laos"
replace country="Luxembourg" if country=="Luxemburg"
replace country="Korea" if country=="S. Korea"
replace country="United Arab Emirates" if country=="U. Arab Emirates"
replace country="Malta" if countrycode=="MLT"


duplicates drop
drop if country==""
compress
save data_for_maps, replace


*** STEP0
* Run next 3 lines once to install needed packages 
*ssc install spmap
*ssc install shp2dta
*ssc install mif2dta

clear all
set more off

capture cd "`path0'maps"

*** STEP1 
* Convert map of all countries into stata format 

shp2dta using ne_10m_admin_0_countries, database(world_db) coordinates(world_coord) genid(id) replace


*** STEP2
* create bridge for merging map and unemployment data 
use world_db, clear


keep NAME id
duplicates drop
gen country=NAME
replace country = proper(country)
replace country="Ivory Coast" if country=="CôTe D'Ivoire"


drop if country==""
save world_db, replace

*** STEP3
* Attach the data to the data with regions' IDs

di "`path0'workfiles\data_for_maps.dta"
joinby country using "`path0'workfiles\data_for_maps.dta", unmatched(both)
duplicates report country


*** STEP4
spmap idv using world_coord if NAME~="Antarctica", ///
	id(id) fcolor(Rainbow) legstyle(3) clnumber(10) ///
	clbreaks(0 10 20 30 40 50 60 70 80 90 100) ///
legend(label(1 "No data") label(2 "0-9") label(3 "10-19") label(4 "20-29") ///
		label(5 "30-39") label(6 "40-49") label(7 "50-59") label(8 "60-69") ///
		label(9 "70-79") label(10 "80-89") label(11 "90-99"))
		

spmap distM_UK using world_coord if NAME~="Antarctica", ///
	id(id) fcolor(Rainbow) legstyle(3) clnumber(11) ///
	clbreaks(0 .5482754 .9206058 1.313591 1.503787 1.722486 1.982948 2.065917 2.195491 2.366432 2.801838 3.596728) ///
legend(	label(1 "No data") label(2 "0.00 - 0.54") label(3 "0.55 - 0.92") ///
		label(4 "0.93 - 1.31") label(5 "1.32 - 1.50") label(6 "1.51 - 1.72") ///
		label(7 "1.72 - 1.98") label(8 "1.98 - 2.06") label(9 "2.07 - 2.19") ///
		label(10 "2.20 - 2.36") label(11 "2.37 - 2.80") label(12 "2.81 - 3.59")) name(x3, replace)
		
		
		
